package at.mri.dogwatch.client.pages.newestfeed;

import java.util.List;

import at.mri.dogwatch.shared.proxy.VideoProxy;
import at.mri.dogwatch.shared.services.VideoRequestFactory;
import at.mri.dogwatch.shared.services.VideoRequestFactory.VideoRequestContext;

import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class LastVideosDataProvider extends AsyncDataProvider<VideoProxy> {

	
	
	public LastVideosDataProvider(VideoRequestFactory requestFactory) {
		super();
		this.requestFactory = requestFactory;
	}

	
	private VideoRequestFactory requestFactory;
	
	@Override
	protected void onRangeChanged(HasData<VideoProxy> display) {
		Range visibleRange = display.getVisibleRange();

		final int start = visibleRange.getStart();
		int length = visibleRange.getLength();
		if (length > 0) {
			// query the data asynchronously
			VideoRequestContext videoRequest = requestFactory.videoRequest();
			videoRequest.listLastModified(length).fire(new Receiver<List<VideoProxy>>() {

				@Override
				public void onSuccess(List<VideoProxy> response) {
					updateRowData(start, response);
				}
				
				@Override
				public void onFailure(ServerFailure error) {
					System.out.println(error.toString());
				}
			});
		}

	}
	
}
